package com.yiren.dbaa.module.exam.api.impl;

import android.app.Activity;

import com.yiren.backstage.config.enums.SiteConfigCodeEnum;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.cache.LineInfoCache;
import com.yiren.dbaa.module.exam.api.IExamLineApi;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/7/14
 */
public class ExamLineApiImpl implements IExamLineApi {

    private Activity activity;

    public ExamLineApiImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public List<LineInfoEntity> loadLineInfoList() {
        List<LineInfoEntity> lineInfos = LineInfoCache.getAllLineInfo();
        if (null == lineInfos) {
            lineInfos = new ArrayList<>();
        }
        return lineInfos;
    }

    @Override
    public List<String> getRandomLines() {
        List<String> randomLineIds = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L28.toString());
        if (null == randomLineIds) {
            randomLineIds = new ArrayList<>();
        }
        return randomLineIds;
    }

    @Override
    public List<LineInfoEntity> loadLineInfos() {
        List<LineInfoEntity> lineInfos = LineInfoCache.getAllLineInfo();
        if (null != lineInfos) return lineInfos;
        else return new ArrayList<>();
    }

    @Override
    public LineInfoEntity getOneLineInfo(List<LineInfoEntity> lineInfos) {
        boolean isRandomPlayLine = (boolean) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L27.toString());
        if (isRandomPlayLine) {
            return getRandomLineInfo(lineInfos);
        } else {
            return getSelectLineInfo(lineInfos);
        }
    }

    @Override
    public LineInfoEntity getRandomLineInfo(List<LineInfoEntity> lineInfos) {
        LineInfoEntity selectLineInfo = getOneRandomLineInfo(lineInfos);
        if (selectLineInfo != null) {
            return selectLineInfo;
        } else {
            if (null != lineInfos && lineInfos.size() > 0) {
                return lineInfos.get(0);
            }
        }
        return null;
    }

    @Override
    public LineInfoEntity getSelectLineInfo(List<LineInfoEntity> lineInfos) {
        String lineId = (String) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L29.toString());
        if (StringUtils.isNotEmpty(lineId)) {
            for (int i = 0; i < lineInfos.size(); i++) {
                if (lineInfos.get(i).getId().equals(lineId)) {
                    return lineInfos.get(i);
                }
            }
        } else {
            if (null != lineInfos && lineInfos.size() > 0) {
                return lineInfos.get(0);
            }
        }
        return null;
    }

    @Override
    public LineInfoEntity getOneRandomLineInfo(List<LineInfoEntity> lineInfos) {
        List<String> randomLines = (List<String>) SignalGConstant.examConfigMap.get(SiteConfigCodeEnum.L28.toString());
        String lineId = null;
        if (null != randomLines) {
            if (randomLines.size() == 1) {
                lineId = randomLines.get(0);
            } else {
                lineId = randomLines.get(new Random().nextInt(randomLines.size()));
            }
            for (LineInfoEntity lineInfo : lineInfos) {
                if (lineId.equals(lineInfo.getId())) {
                    return lineInfo;
                }
            }
        }
        return null;
    }
}
